package com.wealth.system.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wealth.bbs.vo.TopicVO;
import com.wealth.commodity.entity.Goods;
import com.wealth.commodity.service.IGoodsService;
import com.wealth.merchant.entity.Market;
import com.wealth.merchant.service.IMarketService;
import com.wealth.page.entity.Ad;
import com.wealth.page.entity.Floor;
import com.wealth.page.enums.AdTypeEnum;
import com.wealth.page.service.IADService;
import com.wealth.page.service.IFloorService;
import com.wealth.system.web.controller.BaseController;

import iaas.enums.StatusEnum;
import iaas.utils.ResultObject;

/**
 * 首页入口
 * 
 * @author chan
 */
@Controller
@RequestMapping(value="/index")
public class IndexController extends BaseController{

	Logger logger = Logger.getLogger(IndexController.class);
	
	@Autowired
	private IMarketService marketService;
	
	@Autowired
	private IADService adService;
	
	@Autowired
	private IFloorService floorService;
	
	@Autowired
	private IGoodsService goodsService;
	
	/** 
	 * 首页
	 */
	@RequestMapping(value="")
	public String index(Model model,HttpServletRequest request) {
		try{
			//加载市场，惠生活，本地购等
			List<Market> marketList = marketService.findByPropertyName("status", StatusEnum.NORMAL.getValue(), Market.class);
			model.addAttribute("marketList", marketList);
			
			//广告
			List<Ad> adList = adService.getAdByCorpIdAndTypeAndStatus(corpid, AdTypeEnum.INDEX.getValue(), StatusEnum.NORMAL.getValue());
			model.addAttribute("adList", adList);
			
			//广告第二区
			List<Ad> adListTwo = adService.getAdByCorpIdAndTypeAndStatus(corpid, AdTypeEnum.INDEXTWO.getValue(), StatusEnum.NORMAL.getValue());
			model.addAttribute("adListTwo", adListTwo);

			//楼层
			List<Floor> floorList = floorService.getFloorByCorpIdAndStatus(corpid, StatusEnum.NORMAL.getValue());
			model.addAttribute("floorList", floorList);
			return "index";
		}catch(Exception e){
			logger.error("首页错误", e);
			return "system/500";
		}
	}
	
	/** 
	 *特价商品加载
	 */
	@RequestMapping(value="/more",method = RequestMethod.POST)
	@ResponseBody
	public ResultObject topicAjax(
			@RequestParam(defaultValue="-1") Integer index,
			@RequestParam(defaultValue="10") Integer offset) {
		try {
			List<Goods> volist = goodsService.getCheapGoodsList(corpid, index, offset);
			return new ResultObject("000","特价商品加载更多成功", volist);
		} catch (Exception e) {
			return new ResultObject("999","特价商品加载更多错误", e.getMessage());
		}
	}
	
	
	/** 
	 * 首页第二个分类跳转链接
	 */
	@RequestMapping(value="/second")
	public String second(Model model,HttpServletRequest request) {
		return "second";
	}
	
}
